package com.cdtu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdtu.entity.SysDept;
import com.cdtu.entity.SysMenu;
import com.cdtu.entity.SysRole;
import com.cdtu.entity.SysRoleMenu;
import com.cdtu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : Sakura
 * @Date : 2023/8/8 16:31
 * @Description : TODO 用来接收emp的请求
 */
@Controller
@RequestMapping("/role")
//解决跨域问题
@CrossOrigin
public class SysRoleController extends BaseController{

    @Autowired
    private SysEmpRoleService sysEmpRoleService;

    @Autowired
    private SysEmployeeService sysEmployeeService;

    @Autowired
    private SysDeptService deptService;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysRoleMenuService roleMenuService;

    @RequestMapping("/list")
    public ModelAndView empList(ModelAndView modelAndView) {

        List<SysRole> list = roleService.list();
        //返回数据
        modelAndView.addObject("roleList", list);
        //2.跳转页面;
        modelAndView.setViewName("system/sysRole/roleList");
        return modelAndView;
    }

    @RequestMapping("/delete/{roleId}")
    public ModelAndView delete(@PathVariable("roleId") Long roleId, ModelAndView modelAndView) {

        boolean remove = roleService.removeById(roleId);

        modelAndView.setViewName("redirect:/role/list");
        return modelAndView;
    }

    @RequestMapping("/addRole")
    public ModelAndView addEmpUI(SysRole sysRole, ModelAndView modelAndView) {

        roleService.save(sysRole);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/role/list");
        return modelAndView;
    }

    @RequestMapping("/toUpdateUI/{roleId}")

    public ModelAndView toUpdateUi(ModelAndView modelAndView,@PathVariable("roleId") Long roleId) {
        SysRole sysRole = roleService.getById(roleId);
        //返回数据
        modelAndView.addObject("sysRole", sysRole);

        getDeptAndRoleList(modelAndView);
        LambdaQueryWrapper<SysRoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //找出我的级目录
        roleMenuLambdaQueryWrapper.eq(SysRoleMenu::getRoleId, roleId);
        List<SysRoleMenu> menuList = roleMenuService.list(roleMenuLambdaQueryWrapper);
        List<Long> menuIdList = menuList.stream().map(item -> {
            return item.getMenuId();
        }).collect(Collectors.toList());

        List<SysMenu> mySysMenuList = null;
        if (menuIdList.size() > 0) {
            mySysMenuList = menuService.listByIds(menuIdList);
        }
        //再找出所有的目录

        List<SysMenu> allMenuList = menuService.list();

        allMenuList = allMenuList.stream().filter(item->{
            return item.getType() == 2;
        }).collect(Collectors.toList());

        modelAndView.addObject("mySysMenuList", mySysMenuList);
        modelAndView.addObject("allMenuList", allMenuList);
        //2.跳转页面;
        modelAndView.setViewName("system/sysRole/updateSysRole");
        return modelAndView;
    }

    @RequestMapping("/toAddUI")
    public ModelAndView toAddSysRole(ModelAndView modelAndView) {

        getDeptAndRoleList(modelAndView);

        //2.跳转页面;
        modelAndView.setViewName("system/sysRole/addSysRole");
        return modelAndView;
    }

    private void getDeptAndRoleList(ModelAndView modelAndView) {
        List<SysDept> list = deptService.list();
        //返回数据
        modelAndView.addObject("deptList", list);

        //将角色列表查询出来传送到前端给前端人员选择
        List<SysRole> roleList = roleService.list();
        modelAndView.addObject("roleList", roleList);
    }

    @Transactional
    @RequestMapping("/update")
    public ModelAndView update(ModelAndView modelAndView, SysRole sysRole, Long[] menuIds) {
        //先将原来的权限删除
        LambdaQueryWrapper<SysRoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenu::getRoleId, sysRole.getRoleId());
        roleMenuService.remove(lambdaQueryWrapper);

        //删除之后添加新的权限
        for (Long menuId : menuIds) {
            SysMenu sysMenu = menuService.getById(menuId);
            //添加之前先判断父级权限存不存在，存在不管，不存在就添加
            LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper();
            wrapper.eq(SysRoleMenu::getRoleId,sysRole.getRoleId());
            wrapper.eq(SysRoleMenu::getMenuId, sysMenu.getfMenuId());

            SysRoleMenu fMenu = roleMenuService.getOne(wrapper);
            //如果子权限的父menu没有被加进去，可以先加父级menu
            if (fMenu == null) {
                SysRoleMenu fMenus = new SysRoleMenu();
                fMenus.setRoleId(sysRole.getRoleId());
                fMenus.setMenuId(sysMenu.getfMenuId());
                roleMenuService.save(fMenus);
            }

            //添加完父id再添加子id
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(sysRole.getRoleId());
            roleMenu.setMenuId(menuId);
            roleMenuService.save(roleMenu);
        }

        boolean update = roleService.updateById(sysRole);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/role/list");
        return modelAndView;
    }
}
